//Copyright (C) 2011 Ron Coleman. Contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

package bj.actor
import scala.actors.Actor
import scala.actors.OutputChannel
import bj.card.Hand
import bj.hkeeping.Ok
import bj.card.Card
import bj.util.Logs
import bj.hkeeping.Broke

/** This object represents the player's class variables */
object Player {
  /** Unique id counter for players */
  var id: Int = -1

  /** Creates, starts, and send the players on their way.  */
  def start(players : List[Player]) {   
    players.foreach { p =>
      p.start
      
      p ! Go
    }
  }
}

/**
 * This class implements the functionality of a player.
 * @param name Name of the player
 * @param bankroll Bankroll of the player to start
 * @param betAmt Minimum amount player will bet
 */
class Player(playerName: String, beginningBankroll: Double, betAmt: Double) extends Actor with Logs {
  /**
   * Get the player's unique id
   * Note: this assumes players are constructed serially!
   */
  Player.id += 1  
  val pid = Player.id
  var active = true
  var count = 0
  var cardsSeen = 0
  val DECKS_IN_SHOE = 6
  var lastBet = betAmt
  var minBet = 0.0
  var maxBet = 0.0
  var moneyOnTable = 0.0
  var currentBankroll = beginningBankroll
  def bankroll = currentBankroll
  def bankroll_ (value : Double) : Unit = currentBankroll = value
  
  var hand = List[Object with Hand]()
  var handIndex = 0

  /** Dealer's up-card */
  var upcard : Card = _

  /** Table id I've been assigned */
  var tableId : Int = -1
  /** My seat position at my table */
  var seatId : Int = -1
  
  def name = playerName

  /** Pretty-prints the player reference */
  override def toString: String = "(" + name + ", " + pid + ")"

  /** This method receives messages */
  def act {
    loop {
      react {
        // This class is subclassed, so all messages will be acted on by subclasses
        case dontKnow =>
          // Got something we REALLY didn't expect
          debug(this+" received unexpected: "+dontKnow)
      }
    }

  }
 
  /**
   * Processes table numbers.
   * @param tid Table id
   */
  def assign(tid : Int) {
    tableId = tid
  }
  
  /**
   * Observes a card being dealt.
   * Note: This method needs to be overridden if counting cards.
   * @param card Card the player received 
   * @param player Player id receiving this card
   * @param size Shoe size
   */
  
  /** Places a bet with the house */
  def bet {
    if (bankroll < betAmt)
      return

    House ! Bet(pid, betAmt)
  } 
  
  /** 
   * Returns player info in an array for GUI
   */
  def toArray(): Array[AnyRef] = {
    var array : Array[AnyRef] = new Array(4)
    var playerNum = 0
    array(0) = name
    if (tableId != -1)
    	array(1) = ""+tableId
    	else
    	  array(1) = "None"
    array(2) = String.format("%3.2f", double2Double(bankroll))
    array(3) = (getClass.getSimpleName)	

      debug(array(0)+" "+array(1)+" "+array(2)+" "+array(3))
    array
  }
}